package com.shaoshaohuo.app.ui.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.Home_Pick_Adapter;
import com.shaoshaohuo.app.entity.PickEntity;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.NewPickDetails;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.view.LoadListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 采摘
 * Created by Administrator on 2017/6/6.
 */

public class HomePickFragment extends Fragment {
    private String url;
    private View view;
    private LoadListView pick_listView;
    private Home_Pick_Adapter adapter;
    private List<PickEntity.DataBean> dataBeanList;
    private Mybrocatrecier mybrocatrecier;

    public HomePickFragment() {

    }

    private int page = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
private Activity activity;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity=activity;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_pick_acivity, container, false);
        url = getArguments().getString("url");
//        Toast.makeText(getActivity(), url, Toast.LENGTH_SHORT).show();
        mybrocatrecier = new Mybrocatrecier();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("address");
        getActivity().registerReceiver(mybrocatrecier, intentFilter);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mybrocatrecier != null) {
            getActivity().unregisterReceiver(mybrocatrecier);
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        bindAdapter();
        network();
    }

    /**
     * 网络请求
     */
    private void network() {
        page += 1;
        Log.e("TAG", "network: "+page );
        String city = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.CITY_Name, "青岛市");
        String lat = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.CIty_latitude, "36.094406");
        String longtude = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.CIty_longitude, "120.369557");
        ShopHttpConfig.Pick(getActivity(), longtude, lat, "", city, page + "", new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {
            }

            @Override
            public void error(Call call, int code) {
            }

            @Override
            public void suseff(Call call, Response response, Object o) {
//                dataBeanList.clear();
            dataBeanList = ((PickEntity) o).getData();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (page == 1) {
                            adapter.setdate(dataBeanList);

                        }else {
                            adapter.adddate(dataBeanList);
                        }
                        pick_listView.stopload("加载完成", true);
                    }
                });
            }
        }, PickEntity.class);
    }

    private void bindAdapter() {
        pick_listView.setAdapter(adapter);
    }

    private void initView() {
        pick_listView = (LoadListView) view.findViewById(R.id.pick_listView);
        pick_listView.setLoadListener(new LoadListView.LoadListener() {
            @Override
            public void loadlistener() {
                network();
            }
        });
        dataBeanList = new ArrayList<>();
        adapter = new Home_Pick_Adapter(getActivity(), dataBeanList);
        pick_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                List<PickEntity.DataBean> getdate = adapter.getdate();
                intent.setClass(getContext(), NewPickDetails.class);
                intent.putExtra("catid", getdate.get(position).getCatid());
                intent.putExtra("distance", getdate.get(position).getDistance() + "");
                intent.putExtra("imgurl", getdate.get(position).getImg());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 0;
    }

    class Mybrocatrecier extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("address")) {
                page=1;
                String city = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.CITY_Name, "青岛市");
                String lat = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.CIty_latitude, "36.094406");
                String longtude = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.CIty_longitude, "120.369557");
                ShopHttpConfig.Pick(getActivity(), longtude, lat, "", city, page + "", new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {

                    }

                    @Override
                    public void error(Call call, int code) {

                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {
//                dataBeanList.clear();
                        dataBeanList = ((PickEntity) o).getData();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.setdate(dataBeanList);
                                pick_listView.stopload("加载完成", true);

                            }
                        });
                    }
                }, PickEntity.class);
            }
        }
    }
}
